package com.yesep.learn.mqtt.client.handler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.yesep.learn.mqtt.client.common.util.ChannelUtil;
import com.yesep.learn.mqtt.client.common.util.FixHeaderUtil;

import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.mqtt.MqttMessage;
import io.netty.handler.codec.mqtt.MqttMessageType;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;

/**
 * 
 * @Description:心跳处理器,一定时间内没有读写操作则发送心跳
 */
public class HeartBeatHandler extends ChannelDuplexHandler {

	private Logger log = LoggerFactory.getLogger(getClass());

	@Override
	public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
		if (evt instanceof IdleStateEvent && ((IdleStateEvent) evt).state() == IdleState.ALL_IDLE) {
			ctx.channel().writeAndFlush(new MqttMessage(FixHeaderUtil.from(MqttMessageType.PINGREQ)));
			log.debug("send heartbeat : {}", ChannelUtil.clientId(ctx.channel()));
		} else {
			ctx.fireUserEventTriggered(evt);
		}
	}
}
